package com.sunyur.tailor.channel.service.inner.org.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.org.model.UpstreamOrgModel;
import com.sunyur.tailor.channel.service.inner.org.param.OrgSyncInfoParam;
import com.sunyur.tailor.channel.service.inner.org.param.UpstreamOrgParam;
import com.sunyur.tailor.channel.service.inner.org.service.SunyurOrgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *
 * @Author 99288
 * @Description 组织实现
**/
@Slf4j
@Service("sunyurOrgService")
public class SunyurOrgServiceImpl implements SunyurOrgService{

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 组织信息同步 链接
     */
    @Value("${sunyur.api.syncOrgInfoList}")
    private String syncOrgInfoList;


    @Value("${sunyur.api.getUpstreamOrg}")
    private String getUpstreamOrg;

    /**
     * @ClassName SunyurOrgServiceImpl
     * @Description 同步组织给主线
     * @Author yu.chen
     * @Date 2021/4/29 3:36
     * @Version v1.0
     */
    @Override
    public ChannelResult syncOrgList(List<OrgSyncInfoParam> orgSyncInfoParamList){
        if (CollectionUtils.isEmpty(orgSyncInfoParamList)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        // 调用主线接口
        SunyurResult<JSONArray> sunyurResult = sunyurClient.execPost(syncOrgInfoList, JSON.toJSONString(orgSyncInfoParamList), null);
        if (StringUtils.isEmpty(sunyurResult.getErrorMessage()) && CollectionUtils.isEmpty(sunyurResult.getContent())) {
            return ChannelResultUtil.success(Boolean.TRUE);
        }
        log.error("sync org to sunyur failed：{}", JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage() + sunyurResult.getContent());
    }



    @Override
    public ChannelResult<UpstreamOrgModel> getUpstreamOrg(UpstreamOrgParam upstreamOrgParam) {
        log.info("SunyurPurchaseApplyServiceImpl getUpstreamOrg param is {}", JSON.toJSONString(upstreamOrgParam));
        if (ObjectUtils.isEmpty(upstreamOrgParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurPurchaseApplyServiceImpl getUpstreamOrg param is null");
        }
        // 调用主线查询采购单头信息接口
        SunyurResult<UpstreamOrgModel> sunyurResult = sunyurClient.execPost(getUpstreamOrg, JSON.toJSONString(upstreamOrgParam), new TypeReference<SunyurResult<UpstreamOrgModel>>() {});
        // 主线返回错误
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }
}
